package com.ciwong.epaper.util;

import android.widget.Toast;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.common.guidedialog.ChooseReadDialog;
import com.ciwong.epaper.modules.epaper.bean.CatalogueInfo;
import com.ciwong.epaper.modules.epaper.bean.EpaperInfo;
import com.ciwong.epaper.modules.epaper.bean.Module;
import com.ciwong.epaper.modules.epaper.bean.ModuleContent;
import com.ciwong.epaper.modules.epaper.bean.ModuleInfo;
import com.ciwong.epaper.modules.epaper.dao.EpaperRequest;
import com.ciwong.epaper.modules.epaper.util.EpaperJumpManager;
import com.ciwong.epaper.modules.epaper.util.ListenSpeakUtil;
import com.ciwong.epaper.modules.interactivetutorial.InteractiveTutorialCordovaActivity;
import com.ciwong.epaper.modules.me.bean.CheckValidBean;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.modules.onlineanswer.OnlineAnswerCordovaActivity;
import com.ciwong.epaper.modules.viedoexplantion.util.ViedoJumpManager;
import com.ciwong.epaper.util.download.DownLoadInfo;
import com.ciwong.libs.widget.CWToast;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.ToastUtil;

import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2016/11/25.
 */
public class ServicePermissionJudgeUtil {

    private static final String TAG = "ServicePermissionJudgeU";

    public static void servicePermissionFilter2(final BaseActivity activity, final DownLoadInfo downLoadInfo,
                                                final Module mModule, EpaperInfo epaperInfo, final ModuleContent moduleContent,
                                                final int serviceId, List<EpaperInfo.Server> mEpaperServerList, final int position, boolean isPreView,
                                                boolean isFromDesk, final BaseCallBack baseCallBack) {


        try {
            final int moduleId;
            if (downLoadInfo != null && downLoadInfo.getType() == 1) {//new  type
                moduleId = Integer.parseInt(downLoadInfo.getModuleId());

                if (moduleId == ModuleInfo.ModuleInfoId.MODULE_ID_VIDEO_EXPLAIN_NEW || moduleId == ModuleInfo.ModuleInfoId.MODULE_ID_VIDEO_EXPLAIN) {
                    ViedoJumpManager.jumpToNewVideoExplationActivityPreView(R.string.go_back, activity, downLoadInfo, null, 0, null, null, 0, isPreView, epaperInfo);
                    activity.hideDownloadProgress();
                    return;
                }
            } else {
                moduleId = mModule.getModuleInfo().getModuleId();

                if (moduleId == ModuleInfo.ModuleInfoId.MODULE_ID_VIDEO_EXPLAIN_NEW || moduleId == ModuleInfo.ModuleInfoId.MODULE_ID_VIDEO_EXPLAIN) {
                    ViedoJumpManager.jumpToNewVideoExplationActivity(R.string.go_back, activity, downLoadInfo, mModule, position, null, moduleContent.getResourceName(), serviceId, mEpaperServerList, epaperInfo, null);

                    activity.hideDownloadProgress();
                    return;
                }
            }


            String strServiceId;

            if (serviceId == -1) {
                //扫一扫进入
                strServiceId = "";
            } else {
//            书桌进入
                strServiceId = serviceId + "";
            }
            if (isFromDesk) {
                String resourceType;
                if (downLoadInfo != null && downLoadInfo.getType() == 1) {
                    resourceType = downLoadInfo.getResourceType();
                } else {
                    resourceType = moduleContent.getResourceType();
                }
                if (!ListenSpeakUtil.isOnLineAnswerOrListenSpeakOrInteractiveTutorial(moduleId)) {
                    activity.hideDownloadProgress();
                }

                switch (moduleId) {
                    // 同步测试
                    case ModuleInfo.ModuleInfoId.MODULE_ID_TEST:
                        String packagesJsonPath;
                        if (downLoadInfo.getType() == CatalogueInfo.PackageMode.PACKAGE_MODE_NEW) {
                            packagesJsonPath = ESystem.getPackagesJsonPathNew(downLoadInfo.getBookId(), downLoadInfo.getChapterId(), downLoadInfo.getModuleId(), downLoadInfo.getVersionId());

                        } else {
                            packagesJsonPath = ESystem.getPackagesJsonPath(
                                    moduleContent.getResourceFile());
                        }

                        if (new File(packagesJsonPath).exists()) {
                            EpaperJumpManager.jumpToDictaion(
                                    R.string.go_back, activity, downLoadInfo,
                                    mModule, position);
                        } else {
                            CWToast.makeText(activity, R.string.offline_answer,
                                    Toast.LENGTH_LONG, true)
                                    .setToastType(CWToast.TOAST_TYPE_SUC)
                                    .show();
                        }
                        break;

                    // 听说模考
                    case ModuleInfo.ModuleInfoId.MODULE_ID_LISTEN_SPEAK_EXAM:
                        if (DialogUtil.sdIsFull(downLoadInfo)) {
                            DialogUtil.showSdFullDialog(activity);
                            return;
                        }
                        int lsMode = ListenSpeakUtil.LsMode.OWN_PRACTISE;

                        if (downLoadInfo.getType() == CatalogueInfo.PackageMode.PACKAGE_MODE_NEW) {

                            EpaperJumpManager.jumpToListenSpeakWorkNew(R.string.go_back,
                                    activity,false, "0", 0, downLoadInfo, null, "", lsMode, serviceId, null, 0, 0);
                        } else {

                            EpaperJumpManager.jumpToListenSpeakWork(R.string.go_back,
                                    activity, "0", downLoadInfo, mModule, position, null,
                                    " ", "", lsMode, serviceId, null, 0, 0,false);
                        }

                        break;

                    // 同步跟读
                    case ModuleInfo.ModuleInfoId.MODULE_ID_REPEAT:
                        if (DialogUtil.sdIsFull(downLoadInfo)) {
                            DialogUtil.showSdFullDialog(activity);
                            return;
                        }
                        ChooseReadDialog dialog = new ChooseReadDialog(activity, "0",
                                downLoadInfo, mModule, position, resourceType,
                                -1, serviceId,-1);
                        dialog.show();
                        break;
                    // 新资源区分
                    // 句子跟读
                    case ModuleInfo.ModuleInfoId.MODULE_ID_SENTENCE_REPEAT:
                        if (DialogUtil.sdIsFull(downLoadInfo)) {
                            DialogUtil.showSdFullDialog(activity);
                            return;
                        }
                        ChooseReadDialog dialog2 = new ChooseReadDialog(activity, "0",
                                downLoadInfo, mModule, position, resourceType,
                                -1, serviceId,-1);
                        dialog2.show();
                        break;
                    // 报听写
                    case ModuleInfo.ModuleInfoId.MODULE_ID_DICTATION:
                        String classId = "";
                        if (EApplication.getInstance().getClazz() != null) {
                            classId = String.valueOf(EApplication.getInstance().getClazz().getClassId());
                        }
                        EpaperJumpManager.jumpToDictaionNew(R.string.go_back, activity, "0", downLoadInfo, mModule, position, -1, classId, null, serviceId, false, null,-1);
                        break;

                    // 时文
                    case ModuleInfo.ModuleInfoId.MODULE_ID_ESSAY:

                        MeJumpManager.jumpToTimeNewsH5Activity(R.string.go_back, activity, downLoadInfo, mModule, position);
                        break;

                    // 在线作答
                    case ModuleInfo.ModuleInfoId.MODULE_ID_ONLINE_ANSWER:
                        if (moduleContent != null) {
                            downLoadInfo.mJsonResPath = ESystem.getPackagesJsonPath(moduleContent.getResourceFile());
                        }
                        // MeJumpManager.jumpToOnlineAnswerCordovaActivity(activity, downLoadInfo, position, OnlineAnswerCordovaActivity.FROM_DESK, "", 0, MainActivity.REQUEST_CODE_TO_DO_WORK);
                        MeJumpManager.jumpToOnlineAnswerCordovaActivity2(activity, moduleContent, mModule, downLoadInfo, position, serviceId, OnlineAnswerCordovaActivity.FROM_DESK, "", 0, RequestCode.MainActivityResquestCode.REQUEST_CODE_TO_DO_WORK);

                        break;
                    //互动教程
                    case ModuleInfo.ModuleInfoId.MODULE_ID_INTERACTIVE_TUTORIAL:
                        if (moduleContent != null) {
                            downLoadInfo.mJsonResPath = ESystem.getPackagesJsonPath(moduleContent.getResourceFile());
                        }
                        MeJumpManager.jumpToInteractiveTutorialCordovaActivity(activity, moduleContent, mModule, downLoadInfo, position, serviceId, InteractiveTutorialCordovaActivity.FROM_DESK, "", 0, RequestCode.MainActivityResquestCode.REQUEST_CODE_TO_DO_WORK);

                        break;
                    default:
                        CWToast.makeText(activity, R.string.unknown_type,
                                Toast.LENGTH_LONG, true)
                                .setToastType(CWToast.TOAST_TYPE_SUC).show();
                        break;
                }

            } else {
                EpaperRequest.checkValid(EApplication.BRAND_ID + "",
                        strServiceId,
                        downLoadInfo.getBookId(),
                        downLoadInfo.getChapterId(),
                        null,  new BaseExtCallBack(activity) {
                            @Override
                            public void failed(int errorCode, Object data) {
                                super.failed(errorCode, data);

                                if (null != data && errorCode != 17 && errorCode != 27) {
                                    if (errorCode == 5201) {
                                        ToastUtil.INSTANCE.toastCenterErrorWithErrorCode(data.toString(), errorCode);
                                    } else {
                                        ToastUtil.INSTANCE.toastCenterNoNetErrorOrServerError();
                                    }
                                }

                                if (null != baseCallBack) {
                                    baseCallBack.failed(errorCode, data);
                                }
                                activity.hideDownloadProgress();
                            }

                            @Override
                            public void failed(Object data) {
                                super.failed(data);
                                ToastUtil.INSTANCE.toastCenterNoNetErrorOrServerError();

                                activity.hideDownloadProgress();
                                if (null != baseCallBack) {
                                    baseCallBack.failed(data);
                                }
                            }

                            @Override
                            public void success(Object data) {
                                if (data == null) {
                                    activity.hideDownloadProgress();
                                } else {

                                    String resourceType;
                                    if (downLoadInfo != null && downLoadInfo.getType() == 1) {
                                        resourceType = downLoadInfo.getResourceType();
                                    } else {
                                        resourceType = moduleContent.getResourceType();
                                    }
                                    if (!ListenSpeakUtil.isOnLineAnswerOrListenSpeakOrInteractiveTutorial(moduleId)) {
                                        activity.hideDownloadProgress();
                                    }


                                    CheckValidBean checkValidBean = (CheckValidBean) data;
                                    if (checkValidBean != null && checkValidBean.isValid == 1) { //有权限
                                        switch (moduleId) {
                                            // 同步测试
                                            case ModuleInfo.ModuleInfoId.MODULE_ID_TEST:
                                                String packagesJsonPath;
                                                if (downLoadInfo.getType() == CatalogueInfo.PackageMode.PACKAGE_MODE_NEW) {
                                                    packagesJsonPath = ESystem.getPackagesJsonPathNew(downLoadInfo.getBookId(), downLoadInfo.getChapterId(), downLoadInfo.getModuleId(), downLoadInfo.getVersionId());

                                                } else {
                                                    packagesJsonPath = ESystem.getPackagesJsonPath(
                                                            moduleContent.getResourceFile());
                                                }

                                                if (new File(packagesJsonPath).exists()) {
                                                    EpaperJumpManager.jumpToDictaion(
                                                            R.string.go_back, activity, downLoadInfo,
                                                            mModule, position);
                                                } else {
                                                    CWToast.makeText(activity, R.string.offline_answer,
                                                            Toast.LENGTH_LONG, true)
                                                            .setToastType(CWToast.TOAST_TYPE_SUC)
                                                            .show();
                                                }
                                                break;

                                            // 听说模考
                                            case ModuleInfo.ModuleInfoId.MODULE_ID_LISTEN_SPEAK_EXAM:
                                                if (DialogUtil.sdIsFull(downLoadInfo)) {
                                                    DialogUtil.showSdFullDialog(activity);
                                                    return;
                                                }
                                                int lsMode = ListenSpeakUtil.LsMode.OWN_PRACTISE;

                                                if (downLoadInfo.getType() == CatalogueInfo.PackageMode.PACKAGE_MODE_NEW) {

                                                    EpaperJumpManager.jumpToListenSpeakWorkNew(R.string.go_back,
                                                            activity,false, "0", 0, downLoadInfo, null, "", lsMode, serviceId, null, 0, 0);
                                                } else {

                                                    EpaperJumpManager.jumpToListenSpeakWork(R.string.go_back,
                                                            activity, "0", downLoadInfo, mModule, position, null,
                                                            " ", "", lsMode, serviceId, null, 0, 0,false);
                                                }

                                                break;

                                            // 同步跟读
                                            case ModuleInfo.ModuleInfoId.MODULE_ID_REPEAT:
                                                if (DialogUtil.sdIsFull(downLoadInfo)) {
                                                    DialogUtil.showSdFullDialog(activity);
                                                    return;
                                                }
                                                ChooseReadDialog dialog = new ChooseReadDialog(activity, "0",
                                                        downLoadInfo, mModule, position, resourceType,
                                                        -1, serviceId,-1);
                                                dialog.show();
                                                break;
                                            // 新资源区分
                                            // 句子跟读
                                            case ModuleInfo.ModuleInfoId.MODULE_ID_SENTENCE_REPEAT:
                                                if (DialogUtil.sdIsFull(downLoadInfo)) {
                                                    DialogUtil.showSdFullDialog(activity);
                                                    return;
                                                }
                                                ChooseReadDialog dialog2 = new ChooseReadDialog(activity, "0",
                                                        downLoadInfo, mModule, position, resourceType,
                                                        -1, serviceId,-1);
                                                dialog2.show();
                                                break;
                                            // 报听写
                                            case ModuleInfo.ModuleInfoId.MODULE_ID_DICTATION:
                                                String classId = "";
                                                if (EApplication.getInstance().getClazz() != null) {
                                                    classId = String.valueOf(EApplication.getInstance().getClazz().getClassId());
                                                }
                                                EpaperJumpManager.jumpToDictaionNew(R.string.go_back, activity, "0", downLoadInfo, mModule, position, -1, classId, null, serviceId, false, null,-1);
                                                break;

                                            // 时文
                                            case ModuleInfo.ModuleInfoId.MODULE_ID_ESSAY:

                                                MeJumpManager.jumpToTimeNewsH5Activity(R.string.go_back, activity, downLoadInfo, mModule, position);
                                                break;

                                            // 在线作答
                                            case ModuleInfo.ModuleInfoId.MODULE_ID_ONLINE_ANSWER:
                                                if (moduleContent != null) {
                                                    downLoadInfo.mJsonResPath = ESystem.getPackagesJsonPath(moduleContent.getResourceFile());
                                                }
                                                // MeJumpManager.jumpToOnlineAnswerCordovaActivity(activity, downLoadInfo, position, OnlineAnswerCordovaActivity.FROM_DESK, "", 0, MainActivity.REQUEST_CODE_TO_DO_WORK);
                                                MeJumpManager.jumpToOnlineAnswerCordovaActivity2(activity, moduleContent, mModule, downLoadInfo, position, serviceId, OnlineAnswerCordovaActivity.FROM_DESK, "", 0, RequestCode.MainActivityResquestCode.REQUEST_CODE_TO_DO_WORK);

                                                break;
                                            //互动教程
                                            case ModuleInfo.ModuleInfoId.MODULE_ID_INTERACTIVE_TUTORIAL:
                                                if (moduleContent != null) {
                                                    downLoadInfo.mJsonResPath = ESystem.getPackagesJsonPath(moduleContent.getResourceFile());
                                                }
                                                MeJumpManager.jumpToInteractiveTutorialCordovaActivity(activity, moduleContent, mModule, downLoadInfo, position, serviceId, InteractiveTutorialCordovaActivity.FROM_DESK, "", 0, RequestCode.MainActivityResquestCode.REQUEST_CODE_TO_DO_WORK);

                                                break;
                                            default:
                                                CWToast.makeText(activity, R.string.unknown_type,
                                                        Toast.LENGTH_LONG, true)
                                                        .setToastType(CWToast.TOAST_TYPE_SUC).show();
                                                break;
                                        }
                                    } else { //无权限

                                        if (serviceId != -1) {
                                            activity.hideDownloadProgress();
                                            MeJumpManager.jumpBuyService(activity, R.string.go_back, serviceId, -1, EpaperConstant.REQ_CODE_EXPIRE_TO_BUY_SERVICE, checkValidBean.msg);
                                        } else {

                                            if (null != baseCallBack) {
                                                activity.hideDownloadProgress();
                                                baseCallBack.success(data);
                                            }
                                        }

                                    }
                                }
                                super.success(data);


                            }
                        });
            }


        } catch (Exception e) {
            e.getStackTrace();
            if (activity != null) {
                activity.hideDownloadProgress();
            }

        }
    }
}

